import os

def print_tree(root_path, indent=""):
    """递归打印目录树"""
    #获取文件和文件夹列表，并排序，文件夹优先
    items = sorted(os.listdir(root_path),key=lambda x:(not os.path.isdir(os.path.join(root_path,x)),x))
    for item,name in enumerate(items):
        path = os.path.join(root_path, name)
        is_last = (item == len(items)-1)
        prefix = "|__" if is_last else "|——"
        print(indent + prefix + name)
        if os.path.isdir(path):
            extension = "    " if is_last else "|   "
            print_tree(path, indent + extension)

if __name__ == '__main__':
    folder = r"D:\..MBA\数学建模\model\源域数据集"
    print(folder)
    print_tree(folder)